package com.example.bzh.gamecat.ui.message;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RelativeLayout;

import com.example.bzh.gamecat.R;
import com.example.bzh.gamecat.ui.message.innerfriend.MessageFriendFragment;
import com.example.bzh.gamecat.ui.message.innermessage.MessageInnerFragment;
import com.example.bzh.gamecat.ui.message.search.SearchActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

public class MessageFragment extends Fragment {

    @BindView(R.id.rb_message)
    RadioButton rbMessage;
    @BindView(R.id.rb_friend)
    RadioButton rbFriend;
    @BindView(R.id.iv_message_search)
    ImageView ivMessageSearch;
    @BindView(R.id.iv_message_join)
    ImageView ivMessageJoin;
    @BindView(R.id.rl_message_top)
    RelativeLayout rlMessageTop;
    @BindView(R.id.ll_message_container)
    LinearLayout llMessageContainer;
    private MessageInnerFragment messageInnerFragemt;
    private MessageFriendFragment messageFriendFragment;

    private static final int MESSAGE = 0;
    private static final int FRIEND = 1;
    private FragmentManager manager;
    private View pop;
    private PopupWindow window;

    public MessageFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_message, container, false);
        ButterKnife.bind(this, view);
        initView();
        rbMessage.setChecked(true);
        selectFragmet(MESSAGE);
        return view;
    }

    private void initView() {
        messageInnerFragemt = new MessageInnerFragment();
        messageFriendFragment = new MessageFriendFragment();
        manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.ll_message_container, messageFriendFragment)
                .add(R.id.ll_message_container, messageInnerFragemt)
                .commit();

        pop = LayoutInflater.from(getActivity()).inflate(R.layout.message_join_popwindow, null);
        window = new PopupWindow(pop, ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setOutsideTouchable(false);
    }

    public void setButtonSelected() {
        rbFriend.setSelected(false);
        rbMessage.setSelected(false);
    }

    public void selectFragmet(int position) {
        setButtonSelected();
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        fragmentTransaction.hide(messageFriendFragment).hide(messageInnerFragemt);
        switch (position) {
            case MESSAGE:
                fragmentTransaction.show(messageInnerFragemt);
                rbMessage.setSelected(true);
                break;
            case FRIEND:
                fragmentTransaction.show(messageFriendFragment);
                rbFriend.setSelected(true);
                break;
        }
        fragmentTransaction.commit();
    }

    @OnClick({R.id.rb_message, R.id.rb_friend, R.id.iv_message_search, R.id.iv_message_join})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rb_message:
                selectFragmet(MESSAGE);
                break;
            case R.id.rb_friend:
                selectFragmet(FRIEND);
                break;
            case R.id.iv_message_search:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
            case R.id.iv_message_join:
                if (window.isShowing()) {
                    window.dismiss();
                } else {
                    window.showAsDropDown(rlMessageTop, 0, 0, Gravity.END);
                }
                break;
        }
    }
}
